package com.ideaaedi.commonds.rank;


import java.util.List;

/**
 * 排名器
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 1.0.0
 */
public class DefaultRankSupporter<T extends Rankable<T>> implements RankSupporter<T> {
    
    @Override
    public List<T> natural(List<T> nonNullSortedList) {
        int rankStartValue = rankStartValue();
        for (int i = 0, size = nonNullSortedList.size(); i < size; i++) {
            nonNullSortedList.get(i).fillRank(i + rankStartValue);
        }
        return nonNullSortedList;
    }
    
    @Override
    public List<T> indexPostponedIfSame(List<T> nonNullSortedList) {
        // 第一名先排 rankStartValue
        int rankStartValue = rankStartValue();
        T pre = nonNullSortedList.get(0);
        pre.fillRank(rankStartValue);
        // 处理剩下的
        for (int i = 1, size = nonNullSortedList.size(); i < size; i++) {
            T curr = nonNullSortedList.get(i);
            if (pre.compareTo(curr) == 0) {
                curr.fillRank(pre.obtainRank());
            } else {
                curr.fillRank(i + rankStartValue);
            }
            pre = curr;
        }
        return nonNullSortedList;
    }
    
    @Override
    public List<T> rankPostponedIfSame(List<T> nonNullSortedList) {
        // 第一名先排rankStartValue
        int rankStartValue = rankStartValue();
        T pre = nonNullSortedList.get(0);
        pre.fillRank(rankStartValue);
        // 处理剩下的
        for (int i = 1, size = nonNullSortedList.size(); i < size; i++) {
            T curr = nonNullSortedList.get(i);
            int preRank = pre.obtainRank();
            if (pre.compareTo(curr) == 0) {
                curr.fillRank(preRank);
            } else {
                curr.fillRank(preRank + 1);
            }
            pre = curr;
        }
        return nonNullSortedList;
    }
}
